package com.woniuxy.third.utils;

import com.woniuxy.third.entity.User;
import com.woniuxy.third.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class GetUserUtil {
  // 日志对象
  private static final Logger logger = LoggerFactory.getLogger(GetUserUtil.class);

  // 注入服务
  private static UserService userService;
  // 服务赋值
  @Autowired
  public void setConfigService(UserService userService) {
    GetUserUtil.userService = userService;
  }
  // 静态方法
  public static User getUser(HttpServletRequest request) {
    // 得到token
    String token = request.getHeader("authorization");


    //这个if else????????
    if (token.equals("")) {
      logger.debug("未得到token");
    } else {
      logger.debug("得到token");
    }
    token= token.replace("Bearer ", "");
    // 从token得到账户
    String account = JwtUtil.getAccount(token);
    // 根据账户查询到user
    return userService.findByAccount(account);
  }
}
